using System;
using System.Configuration;
using System.IO;
using Microsoft.Health.Samples.PatientConnect.ContosoHealth.HealthVaultAdapter;

public partial class AddPatient : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        AddJavaScript();
    }

    protected void ButtonSave_Click(object sender, EventArgs e)
    {
        // Add new patient details in local database
        String personID = ClinicalDataBaseManager.CreatePatient(Server.MapPath(Constants.PatientXmlFilePath),
                                                                TextBoxLastName.Text.Trim(), 
                                                                TextBoxFirstName.Text.Trim(), 
                                                                TextBoxMiddleName.Text.Trim());

        string identificationCode = string.Empty;

        if (personID != null)
        {
            String friendlyName = TextBoxFirstName.Text.Trim() + " " + TextBoxMiddleName.Text.Trim() + " " + TextBoxLastName.Text.Trim();
            Guid applicationId = new Guid(ConfigurationManager.AppSettings["ApplicationId"].ToString());
            PatientConnectUtilities healthVaultUtilities = new PatientConnectUtilities(applicationId);
            
            identificationCode = healthVaultUtilities.CreatePatient(friendlyName,
                                                                    TextBoxIDQuestion.Text.Trim(),
                                                                    TextBoxIDAnswer.Text.Trim(),
                                                                    personID);
            
            ClinicalDataBaseManager.CreateXmlDataBase(Server.MapPath(Constants.PracticeFolderPath),
                                                      personID,
                                                      Server.MapPath(Constants.SampleCcrCcdFolderName), 
                                                      Server.MapPath(Constants.SampleHL7MessageFolderName));

            Session["Friendlyname"] = friendlyName;
            Response.Redirect("Acknowledgement.aspx?IDcode=" + identificationCode);
        }
    }

    private void AddJavaScript()
    {
        string junkChars = "<,>,&,\",(,),^,!,#,$,%,*,+,=,_,-";
        TextBoxFirstName.Focus();
        TextBoxFirstName.Attributes.Add("Onkeypress",
                                        "CheckSpecialCharacter('" + junkChars + "')");
        TextBoxLastName.Attributes.Add("Onkeypress",
                                        "CheckSpecialCharacter('" + junkChars + "')");
        TextBoxMiddleName.Attributes.Add("Onkeypress",
                                         "CheckSpecialCharacter('" + junkChars + "')");
        TextBoxIDQuestion.Attributes.Add("Onkeypress",
                                         "CheckSpecialCharacter('" + junkChars + "')");
        TextBoxIDAnswer.Attributes.Add("Onkeypress",
                                        "CheckSpecialCharacter('" + junkChars + "')");
    }

    protected void ButtonCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("default.aspx", false);
    }
}
